﻿package Leophy.text {
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.StyleSheet;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	import Leophy.Interfaces.ISay;
	import Leophy.utils.Debug;

	/**
	 * ...
	 * @author Tophy
	 */
	public class MyTextFormat extends StyleSheet implements ISay {

		private static var SS_ins : MyTextFormat;
		private var urlloader : URLLoader ;
		private var _cssloaded:Boolean = false;
		public function get CssLoaded():Boolean {
			return _cssloaded;
		}
		/**
		 * 全局唯一实例
		 */
		public static function get Ins() : MyTextFormat {
			if (SS_ins == null) {
				SS_ins = new MyTextFormat();
			}
			return SS_ins;
		}

		/**
		 * 加载全局样式表
		 * @param	CSSurl
		 */
		public function loadCSS(CSSurl : String) : void {
			if (CSSurl != null && CSSurl != "") {
				if(urlloader == null) {
					urlloader = new URLLoader();
					urlloader.addEventListener(ProgressEvent.PROGRESS, loadingCSS);
					urlloader.addEventListener(Event.COMPLETE, loadedCSS);
				}
				
				var urlrequest : URLRequest = new URLRequest(CSSurl);
				_cssloaded = false;
				urlloader.load(urlrequest);
			}
		}

		public function getTextFormat(styleName : String = "") : TextFormat {
			if (styleName == "" || styleName == null) {
				return null;
			}
			if(this.styleNames.join("").indexOf(styleName) == -1) {
				return null;
			}
			var obj : Object = this.getStyle(styleName);
			
			var align : String = TextFormatAlign.LEFT;
			
			switch(obj.textAlign) {
				case "center":
					align = TextFormatAlign.CENTER;
					break;
				case "right":
					align = TextFormatAlign.RIGHT;
					break;
				case "justify":
					align = TextFormatAlign.JUSTIFY;
					break;
				default:
					align = TextFormatAlign.LEFT;
					break;
			}
			
			
			//Debug.traceObj(obj);
			return new TextFormat(obj.fontFamily, int(delPXstr(obj.fontSize)), parseColorToNumber(obj.color), (obj.fontWeight == "bold"), (obj.fontStyle == "italic"), (obj.textDecoration == "underline"), null, null, align, obj.marginLeft, obj.marginRight, null, obj.leading);
		}

		private function parseColorToNumber(str : String = "") : Number {
			if (str == "" || str == null) return 0;
			str = str.split("#").join("");
			str = "0x" + str;
			return Number(str);
		}

		private function delPXstr(str : String = "") : String {
			if (str.length < 2) return str;
			str = str.split("px").join("");
			str = str.split("pt").join("");
			return str;
		}

		private function loadedCSS(ev : Event) : void {
			//trace(ev.target.data);
			var txt : String = fixSTR(ev.target.data);
			this.parseCSS(txt);
			say(this.styleNames.toString()+this.styleNames.length.toString());
			say("CSS loaded!");
			_cssloaded = true;
			this.dispatchEvent(ev);
		}

		private function fixSTR(str : String = "") : String {
			str = str.split("\"").join("");
			str = str.split("\r\n").join("");
			return str;
		}

		private function loadingCSS(ev : ProgressEvent) : void {
			dispatchEvent(ev);
		}

		public function MyTextFormat() { 
			super(); 
		}

		/* INTERFACE Leophy.Interfaces.ISay */
		public function say(o : *) : void {
			trace("CSS:", o.toString());
		}
	}
}